package com.hgyc.mom.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.system.entity.SysUser;
import com.hgyc.mom.system.service.SysUserService;
import com.hgyc.mom.system.vo.SysUserParam;
import com.hgyc.mom.system.vo.UserInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author fwj
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/sys/user")
@Tag(name = "系统用户接口", description = "系统用户接口")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;


    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Operation(summary = "分页查询用户信息", description = "分页查询用户信息")
    public Result<IPage<SysUser>> list(
            @Parameter(description = "查询参数") @RequestBody SysUserParam sysUserParam) {
        Page<SysUser> page = new Page<>(sysUserParam.getPage(), sysUserParam.getPageSize());
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserParam, sysUser);
        QueryWrapper<SysUser> wrapper = QueryWrapperUtil.build(sysUser);
        wrapper.orderByDesc("status");
        IPage<SysUser> list = sysUserService.page(page, wrapper);
        return Result.success(list);
    }

    @RequestMapping(value = "/role/page", method = RequestMethod.POST)
    @Operation(summary = "分页查询用户信息", description = "分页查询用户信息")
    public Result<IPage<SysUser>> rolePage(
            @Parameter(description = "查询参数") @RequestBody SysUserParam sysUserParam) {
        Page<SysUser> page = new Page<>(sysUserParam.getPage(), sysUserParam.getPageSize());
        IPage<SysUser> list = sysUserService.rolePage(page, sysUserParam);
        return Result.success(list);
    }

    @RequestMapping(value = "/userinfo", method = RequestMethod.GET)
    @Operation(summary = "获取当前用户信息", description = "获取当前用户信息")
    public Result<UserInfoVO> getUserinfo() {
        UserInfoVO userInfo = sysUserService.getUserInfo();
        return Result.success(userInfo);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @Operation(summary = "新增用户", description = "新增用户")
    public Result<?> add(
            @Parameter(description = "系统用户") @RequestBody SysUser sysUser) {
        sysUserService.create(sysUser);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @Operation(summary = "批量删除用户", description = "批量删除用户")
    public Result<?> batchDel(
            @Parameter(description = "删除ID") @RequestParam("ids") List<Long> ids) {
        sysUserService.batchDel(ids);
        return Result.success("删除成功");
    }

    @RequestMapping(value = "/editPwd", method = RequestMethod.POST)
    @Operation(summary = "修改密码", description = "修改密码")
    public Result<?> editPwd(
            @Parameter(description = "系统用户") @RequestBody SysUser sysUser) {
        sysUserService.editPwd(sysUser);
        return Result.success("保存成功");
    }

    @RequestMapping(value = "/queryUserInfo/{userId}", method = RequestMethod.GET)
    @Operation(summary = "根据用户ID获取用户信息", description = "根据用户ID获取用户信息")
    public Result<SysUser> queryUserInfo(
            @Parameter(description = "用户ID") @PathVariable String userId) {
        SysUser userInfo = sysUserService.getById(userId);
        return Result.success(userInfo);
    }

    @RequestMapping(value = "/updateUserinfo", method = RequestMethod.POST)
    @Operation(summary = "更新用户信息", description = "更新用户信息")
    public Result<?> updateUserinfo(
            @Parameter(description = "系统用户") @RequestBody SysUser sysUser) {
        sysUserService.updateUserinfo(sysUser);
        return Result.success("保存成功");
    }
}
